<template>
  <!--会签管理-我的审批-付款计划详情弹窗-表单-->
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false" class="scroll-view" style="height: 600px;">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="审批编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvNumber"
              style="margin-bottom: 10px;">
              <a-input :disabled="true" style="color: black;" v-model="model.approvNumber"
                placeholder="请输入介绍"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="提交时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="submitTime"
              style="margin-bottom: 10px;">
              <j-date v-model="model.submitTime" :showTime="true" date-format="YYYY-MM-DD " style="width: 100%"
                placeholder="请选择提交时间" :disabled="true"></j-date>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="发起人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userName"
              style="margin-bottom: 10px;">
              <a-input :disabled="true" style="color: black;" v-model="model.userName" placeholder="请输入发起人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="职位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="post"
              style="margin-bottom: 10px;">
              <a-select :disabled="true" style="color: black;" placeholder="请选择职位" v-model="model.post">
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departName"
              style="margin-bottom: 10px;">
              <a-input :disabled="true" style="color: black;" placeholder="请选择部门" v-model="model.departName">
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status"
              style="margin-bottom: 10px;">
              <a-select :disabled="true" style="color: black;" placeholder="请选择状态" v-model="model.status">
                <a-select-option value="0">未提交</a-select-option>
                <a-select-option value="1">审核中</a-select-option>
                <a-select-option value="2">已同意</a-select-option>
                <a-select-option value="3">已驳回</a-select-option>
                <a-select-option value="4">已撤销</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name"
              style="margin-bottom: 10px;">
              <a-input :disabled="true" style="color: black;" v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName"
              style="margin-bottom: 10px;">
              <a-select :disabled="true" placeholder="请选择项目" :value="model.projectName"
                @change="handleChange('project',$event)">
                <a-select-option v-for="(item,index) in levelOneOptions" :key="index" :value="item">
                  {{item.label}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>

        
        <a-row>
          <a-col :span="24">
            <div style="margin-left: 30px;line-height: 40px;font-size: 16px;font-weight: 600;">预计收款：</div>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="总金额" :labelCol="labelCol3" :wrapperCol="wrapperCol3" prop="gatheringTotal"
              style="margin-bottom: 10px;margin-left: 20px;">
              <a-input :disabled="true"  v-model="model.gatheringTotal" style="width: 314px;"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="劳务等款项" :labelCol="labelCol" :wrapperCol="wrapperCol"
              prop="gatheringPaymentServices" style="margin-bottom: 10px;">
              <a-input :disabled="true"  v-model="model.gatheringPaymentServices"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="劳务工资" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gatheringLaborWages"
              style="margin-bottom: 30px;">
              <a-input :disabled="true"  v-model="model.gatheringLaborWages"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="24">
            <div style="margin-left: 30px;line-height: 40px;font-size: 16px;font-weight: 600;">对下班组计划支付劳务费：</div>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="总金额" :labelCol="labelCol3" :wrapperCol="wrapperCol3" prop="serviceFeeTotal"
              style="margin-bottom: 10px;margin-left: 20px;">
              <a-input :disabled="true"  v-model="model.serviceFeeTotal" style="width: 314px;"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="劳务等款项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceFeePayment"
              style="margin-bottom: 10px;">
              <a-input :disabled="true"  v-model="model.serviceFeePayment"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="劳务工资" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceFeeLaborWages"
              style="margin-bottom: 30px;">
              <a-input :disabled="true"  v-model="model.serviceFeeLaborWages"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="24">
            <div style="margin-left: 30px;line-height: 40px;font-size: 16px;font-weight: 600;">计划支付材料等款项：</div>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="总金额" :labelCol="labelCol3" :wrapperCol="wrapperCol3"
              prop="gatheringMaterialsTotal" style="margin-bottom: 10px;margin-left: 20px;">
              <a-input :disabled="true"  v-model="model.gatheringMaterialsTotal" style="width: 314px;"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="材料费" :labelCol="labelCol" :wrapperCol="wrapperCol"
              prop="gatheringMaterialsExpense" style="margin-bottom: 10px;">
              <a-input :disabled="true"  v-model="model.gatheringMaterialsExpense"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="机械费" :labelCol="labelCol" :wrapperCol="wrapperCol"
              prop="gatheringMaterialsMachine" style="margin-bottom: 10px;">
              <a-input :disabled="true"  v-model="model.gatheringMaterialsMachine"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="其他" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gatheringMaterialsOther"
              style="margin-bottom: 30px;">
              <a-input :disabled="true"  v-model="model.gatheringMaterialsOther"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="24">
            <div style="margin-left: 30px;line-height: 40px;font-size: 16px;font-weight: 600;">项目部直签支出：</div>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="总金额" :labelCol="labelCol3" :wrapperCol="wrapperCol3" prop="projectTotal"
              style="margin-bottom: 10px;margin-left: 20px;">
              <a-input :disabled="true"  v-model="model.projectTotal"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="劳务费:" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectFeePayment"
              style="margin-bottom: 10px;">
              <a-input :disabled="true"  v-model="model.projectFeePayment"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="机械费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectMachineryCost"
              style="margin-bottom: 10px;">
              <a-input :disabled="true"  v-model="model.projectMachineryCost"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="材料费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectMaterialsExpense"
              style="margin-bottom: 10px;">
              <a-input :disabled="true"  v-model="model.projectMaterialsExpense"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="其他" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectOther"
              style="margin-bottom: 30px;">
              <a-input :disabled="true"  v-model="model.projectOther"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="5">
            <div style="margin-left: 30px;line-height: 40px;font-size: 16px;font-weight: 600;">项目部预留备用资金：</div>
          </a-col>
          <a-col :span="19">
            <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectReservedMoney"
              style="margin-bottom: 30px;">
              <a-input :disabled="true"  v-model="model.projectReservedMoney" style="width: 100%;"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
              style="margin-bottom: 10px;margin-left: 20px;">
              <textarea :disabled="true" v-model="model.remark" cols="100" rows="3" style="width: 93%;"></textarea>
            </a-form-model-item>
          </a-col>
        </a-row>

        <!-- <a-row>
          <a-col :span="12">
            <a-form-model-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status"
              style="margin-bottom: 10px;">
              <a-select placeholder="请选择审核状态" :disabled="true" style="color: black;" v-model="model.status">
                <a-select-option value="0">待审核</a-select-option>
                <a-select-option value="1">已同意</a-select-option>
                <a-select-option value="2">已驳回</a-select-option>
                <a-select-option value="3">已撤销</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row> -->

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="附件资料" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="attachment"
              style="margin:0 60px 10px 20px;">
              <j-upload v-model="model.attachment" :disabled="disabled" :buttonVisible="!disabled"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
        </a-row>

        <a-row>
          <a-form-model-item label="审核流程" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
            style="margin-bottom: 10px;margin-left: 20px;">
            <div style="display: flex;flex-direction: column;width: 100%;">
              <block v-for="(item, index) in list" :key="index">
                <div style="display: flex;flex-direction: column;">
                  <div style="display: flex;align-items: center;">
                    <div style="width:60px;margin-right: 5px;">审批人{{index + 1 }} </div>
                    <div
                      style="border: 0.5px solid gray; width: 674px; padding: 0 5px;display: flex;align-items: center;"
                      :style="{background:((item.approver === approver)&&(item.status === '1')) && pageType ==='approver'?'#fff':'#f5f5f5'}">
                      {{ item.departName }} {{ item.realname }} 意见:<a-input
                        :disabled="((item.approver === approver)&&(item.status === '1')) && pageType ==='approver'?false:true "
                        v-model="item.feedback" placeholder=""
                        style="width: 500px;border: none;background: none;"></a-input>
                    </div>
                    <a-list placeholder="请选择状态" :data-source="list"
                      style="font-size: 16px; width: 80px; margin-left: 5px; display: inline-block">
                      <!-- <a-list-item v-if="item.status == '0'" style="color: blue">待审核</a-list-item> -->
                      <a-list-item v-if="item.status == '1'" style="color: blue">审核中</a-list-item>
                      <a-list-item v-if="item.status == '2'" style="color: green">已同意</a-list-item>
                      <a-list-item v-if="item.status == '3'" style="color: red">已驳回</a-list-item>
                      <a-list-item v-if="item.status == '4'" style="color: gray">已撤销</a-list-item>
                    </a-list>
                  </div>
                  <div style="margin-left: 65px;margin-bottom: 10px;width: 730px;">
                    <j-upload v-model="item.ideaAttachment"
                      :disabled="((item.approver === approver)&&(item.status === '1')&&(item.isAttachment === '1')) && pageType ==='approver'?false:true"
                      :buttonVisible="!(((item.approver === approver)&&(item.status === '1')&&(item.isAttachment === '1')) && pageType ==='approver'?false:true )"></j-upload>
                  </div>
                </div>
              </block>
            </div>
          </a-form-model-item>
        </a-row>

        <a-row>
          <a-button v-if="!(pageType === 'approver'?false:true)"
            style="background: orange;color: white;border:1px solid orange;margin-left: 118px;"
            @click="handleOpen('history')">查看原审核记录</a-button>
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- <div style="width: 200px; height: 50px; margin-left: 550px; margin-top: 5px">
      <a-button @click="bohui" type="primary" style="color: white" v-if="this.status == '1'">驳回</a-button>
      <a-button @click="submit" type="primary" style="color: white; margin-left: 10px"
        v-if="this.status == '1'">同意</a-button>
    </div> -->
    <j-modal class="modal-privacy" :title="title" :visible="visible1" :disableScreenClick="true" okText="确定"
      cancelText="取消" @cancel="cancelPrivacy" @ok="confirmPrivacy">
      <textarea cols="70" rows="5" placeholder="请输入撤销原因"></textarea>
      <!-- <div style="font-size: 20px;width: 200px;height: 20px;margin: 0 auto;margin-top: 10px;">确定提交吗？</div> -->
    </j-modal>
  </a-spin>
</template>

<script>
  import {
    httpAction,
    getAction
  } from '@/api/manage'
  import {
    validateDuplicateValue
  } from '@/utils/util'

  export default {
    name: 'WtBrandForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false,
      },
      defaultModel: {
        type: Boolean,
        default () {
          return {

          }
        },
      }
    },
    data() {
      return {
        title: '撤销原因',
        model: {
          projectId: '',
          status:'',
        },
        visible1: false,
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 2
          },
        },
        wrapperCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 22
          },
        },
        labelCol3: {
          xs: {
            span: 24
          },
          sm: {
            span: 2
          },
        },
        wrapperCol3: {
          xs: {
            span: 24
          },
          sm: {
            span: 8
          },
        },
        confirmLoading: false,
        validatorRules: {},
        levelOneOptions: [],
        treeData: [],
        id: '',
        ids: [],
        status: '',
        idss: '', //提交id
        url: {
          add: '/wtBrand/add',
          edit: '/wtBrand/edit',
          approver: '/countersign/wtCountersignProcess/submitExamineApprove',
          queryById: '/wtBrand/queryById',
        },
        approver: '',
        pageType: '',
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))
      if (!!window.localStorage.getItem("pro__Login_Userinfo")) {
        let userInfo = JSON.parse(window.localStorage.getItem("pro__Login_Userinfo")).value;
        this.approver = userInfo.id;
      }
    },
    mounted() {
      if(!!this.defaultModel.id){
        console.log(222)
        this.model = JSON.parse(JSON.stringify(this.defaultModel));
        this.list = this.model.list;
        getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
          if (res.success) {
            this.treeData = []
            let treeList = res.result
            for (let a = 0; a < treeList.length; a++) {
              let temp = treeList[a]
              temp.isLeaf = temp.leaf
              this.treeData.push(temp)
            }
            this.levelOneOptions = [...this.treeData].map((v) => {
              return {
                ...v,
                value: v.id,
                label: v.departName,
              }
            })
            if (this.levelOneOptions && this.levelOneOptions.length > 0) {
              let info = this.levelOneOptions.find((item) => item.departName == this.model.projectName);
              if(!!info){this.model.projectId = info.id;}
            }
            this.$forceUpdate();
          }
        })
      }
    },
    methods: {
      handleChange(type, item) {
        console.log(1, type, item);
        if (type === 'project') {
          this.model.projectName = item.label;
          this.model.projectId = item.value;
        }
      },
      confirmPrivacy() {
        // console.log('同意了用户协议')
        // console.log(this.privacyDialogShow)l
        this.visible1 = false
        // console.log(this.privacyDialogShow)

      },
      cancelPrivacy() {
        // console.log('拒绝了用户协议')
        this.visible1 = false
      },
      edit(record, type) {
        if (!!type) {
          this.pageType = type;
        }
        // this.model = Object.assign({}, record)
        this.visible = true
        this.loadTree(record)
        this.id = record.id
        this.status = record.status
        // console.log('few',record);
      },
      loadTree(record) {
        var that = this
        getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
          if (res.success) {
            this.treeData = []
            let treeList = res.result
            for (let a = 0; a < treeList.length; a++) {
              let temp = treeList[a]
              temp.isLeaf = temp.leaf
              this.treeData.push(temp)
            }

            this.levelOneOptions = [...this.treeData].map((v) => {
              return {
                ...v,
                value: v.id,
                label: v.departName,
              }
            })
            getAction(`/countersign/wtCountersignPaymentPlan/queryByCountersignId?countersignId=${record.id}`).then(
              (res) => {
                if (res.success) {
                  this.model = res.result;
                  this.list = res.result.list;
                  this.ids = this.list.find(obj => obj.status == '1');
                  this.idss = this.ids.id
                  if (this.levelOneOptions && this.levelOneOptions.length > 0) {
                    let info = this.levelOneOptions.find((item) => item.departName == this.model.projectName);
                    console.log(1, info)
                    this.model.projectId = info.id;
                  }
                }
              }
            )
          }
        })
      },
      add() {
        this.edit(this.modelDefault)
      },

      chexiao() {
        this.visible1 = true
      },
      //查看原审核记录
      handleOpen(type) {
        this.$emit('handleOpen', type);
      },
      submitForm(record) {
        const that = this;
        // debugger
        let httpurl = '';
        let method = '';
        let params = {};
        if (this.pageType === 'approver') { //驳回/同意-审批
          httpurl = this.url.approver;
          method = 'POST';
          params.status = record;
          if (this.list && this.list.length > 0) {
            this.list.forEach(ite => {
              if (this.approver === ite.approver && ite.status === '1') {
                params.feedback = ite.feedback;
                params.ideaAttachment = ite.ideaAttachment;
                params.id = ite.id;
              }
            })
          }
        }

        if (!params.feedback) {
          that.$message.warning('审批意见不能为空,请填写')
        } else {
          that.confirmLoading = true
          httpAction(httpurl, params, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      },

    }
  }
</script>
<style scoped>
  .scroll-view {
    overflow: auto;
    overflow-y: scroll;
  }
</style>